import {
  getSpecsList,
  getSpecsCount
} from '../../../util/axios'
const state = {
  specsList: [],
  size: 2,
  page: 1,
  count: 0
}
const getters = {

  getSpecsList(state) {
    return state.specsList
  },

  getCount(state) {
    return state.count
  },

  getSize(state) {
    return state.size
  }
}
const mutations = {
  REQ_SPECSLIST(state, payload) {
    state.specsList = payload
  },
  REQ_GETCOUNT(state, payload) {
    state.count = payload
  },

  CHANGE_PAGE(state, payload) {
    state.page = payload
  }
}
const actions = {

  getSpecsListAction(context) {
    getSpecsList({
        size: context.state.size,
        page: context.state.page
      })
      .then(res => {
        if (res.data.code == 200) {
          context.commit('REQ_SPECSLIST', res.data.list)
          if (context.state.page != 1 && res.data.list.length == 0) {
            context.dispatch('changePageAction', context.state.page - 1)
          }
        }
      })
  },

  getCountAction({
    commit
  }) {
    getSpecsCount()
      .then(res => {
  
        if (res.data.code == 200) {
          commit('REQ_GETCOUNT', res.data.list[0].total)
        }
      })
  },

  changePageAction(context, payload) {

    context.commit('CHANGE_PAGE', payload)

    context.dispatch('getSpecsListAction')
  }
}


export default {
  state,
  getters,
  mutations,
  actions,
  namespaced: true
}
